﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace WebApplication1
{
    public class ManageRequestPersistence
    {
        static LogicUniversityT2Entities logic = new LogicUniversityT2Entities();
        static Period p = new Period();

        public static DepartmentRequisition generateDepFirstID()
        {
            DepartmentRequisition newDepReq = new DepartmentRequisition();
            string newstrADI = "";

            newstrADI = "DR001";
            newDepReq.DeptRequisitionID = newstrADI;
            logic.SaveChanges();
            return newDepReq;
        }

        public static DepartmentRequisition generateDepID(string strADI)
        {
            DepartmentRequisition newDepReq = new DepartmentRequisition();
            string newstrADI = "";

            newstrADI = strADI.Substring(2);
            int maxStrADI = Convert.ToInt32(newstrADI) + 1;

            newstrADI = string.Format("DR{0:000}", maxStrADI);
            newDepReq.DeptRequisitionID = newstrADI;
            return newDepReq;
        }
        public static DepartmentRequisitionDetail generateDepDetailFirstID()
        {
            DepartmentRequisitionDetail newDepReqDetail = new DepartmentRequisitionDetail();
            string newstrADI = "";

            newstrADI = "DRD001";
            newDepReqDetail.DeptRequisitionID = newstrADI;
            logic.SaveChanges();
            return newDepReqDetail;
        }

        public static DepartmentRequisitionDetail generateDepDetailID(string strADI)
        {
            DepartmentRequisitionDetail newDepReqDetail = new DepartmentRequisitionDetail();
            string newstrADI = "";

            newstrADI = strADI.Substring(3);
            int maxStrADI = Convert.ToInt32(newstrADI) + 1;

            newstrADI = string.Format("DRD{0:000}", maxStrADI);
            newDepReqDetail.DeptRequisitionID = newstrADI;
            return newDepReqDetail;
        }

        public static IQueryable getItemSaleList(Item c)
        {
            var deptReqList = from deptRequisition in logic.DepartmentRequisitions
                              join department in logic.Departments
                              on deptRequisition.DepartmentID equals department.DepartmentID
                              where deptRequisition.ItemID == c.ItemID
                               && deptRequisition.Status == "D"
                              select new
                              {
                                  date = deptRequisition.DeliveredDate,
                                  dept_supplier = department.DepartmentName,
                                  quantity = deptRequisition.AfterDeliveredQuantity,
                              };
            return deptReqList;
        }
        public static IQueryable getEmployeeRequisitionList()
        {
            var empReqList = from er in logic.EmployeeRequisitions
                             join iu in logic.InventoryUsers on er.EmployeeID equals iu.EmployeeID
                             join erd in logic.EmployeeRequisitionDetails on er.EmployeeRequisitionID equals erd.EmployeeRequisitionID

                             where er.DepartmentID == "COMM" && (er.Status == "pending" || er.Status == "sent")
                             group logic.EmployeeRequisitionDetails by new { erd.EmployeeRequisitionID, er.EmployeeRequisitionDate, iu.Name, er.TotalAmount } into abc

                             select new

                             {
                                 RequisitionID = abc.Key.EmployeeRequisitionID,


                                 RequisitonDate = abc.Key.EmployeeRequisitionDate,
                                 EmployeeName = abc.Key.Name,
                                 TotalAmount = abc.Key.TotalAmount
                             };
            return empReqList;

        }
        public static int getNoOfRequest()
        {
            var select = from er in logic.EmployeeRequisitions
                         where er.Status == "pending" || er.Status == "sent"

                         select er;

            int count = select.Count();
            return count;
        }
        public static EmployeeRequisition selectedEmpRequisition(string empReqID)
        {
            EmployeeRequisition selected = (from user in logic.EmployeeRequisitions where user.EmployeeRequisitionID == empReqID select user).FirstOrDefault<EmployeeRequisition>();
            return selected;
        }

        public static void saveToDepartmentRequisitionNDetail(EmployeeRequisition select)
        {
            EmployeeRequisition empReq = logic.EmployeeRequisitions.First<EmployeeRequisition>(emp => emp.EmployeeRequisitionID == select.EmployeeRequisitionID);

            Department dept = logic.Departments.First<Department>(dt => dt.DepartmentID == empReq.DepartmentID);
            string deparmentID = dept.DepartmentID;

            DateTime todayApproveDate = Convert.ToDateTime("2013-02-10");

            saveToDeptReqNDetaill(todayApproveDate, empReq, deparmentID);
        }

        public static void saveToDeptReqNDetaill(DateTime approveDate, EmployeeRequisition empReq, string deparmentID)
        {
            var a = from adid in logic.DepartmentRequisitions select adid;
            DepartmentRequisition deptR1 = new DepartmentRequisition();
            int strReqID = (logic.DepartmentRequisitions.Count()) - 1;
            deptR1 = a.AsEnumerable().ElementAt<DepartmentRequisition>(strReqID);

            DateTime lastappr = Convert.ToDateTime(deptR1.ApproveDate);
            p.Add6Days(lastappr);

            DateTime s = p.StartDate;

            EmployeeRequisitionDetail empReqDetail = empReq.EmployeeRequisitionDetails.First<EmployeeRequisitionDetail>(empreq => empreq.EmployeeRequisitionID == empReq.EmployeeRequisitionID);

            var empDetailID = from detail in logic.EmployeeRequisitionDetails
                              where detail.EmployeeRequisitionID == empReq.EmployeeRequisitionID
                              select new
                              {
                                  empoyeeDetailID = detail.EmployeeRequisitionDetailID,
                                  itemID = detail.ItemID,
                                  reqQty = detail.RequestedQuantity,
                              };

            var list = empDetailID.ToList();
            foreach (var empDetail in list)
            {



                var depList = from dt in logic.DepartmentRequisitions
                              where dt.DepartmentID == deparmentID && dt.ItemID == empDetail.itemID
                              && dt.ApproveDate >= s
                              select new
                              {
                                  deptReq = dt.DeptRequisitionID,
                                  approvedate = dt.ApproveDate,
                                  reqQty = dt.RequestedQuantity
                              };

                var deplist = depList.ToList();
                bool flag;
                if (deplist.Count == 0)
                {
                    flag = false;
                }
                else
                    flag = true;

                if (approveDate > p.StartDate && approveDate < p.EndDate)
                {
                    if (flag)
                    {
                        foreach (var deptl in deplist)
                        {

                            string deptReqID = deptl.deptReq;
                            int qty = Convert.ToInt32(empDetail.reqQty);
                            updateQtyForExistingDeptReqWithinPeriod(deptReqID, qty);
                        }
                    }
                    else
                    {

                        string itemID = empDetail.itemID;
                        int qty = Convert.ToInt32(empDetail.reqQty);
                        addRecordForNewDeptReqWithinPeriod(deparmentID, itemID, qty, empReq);
                    }

                }
                else
                {

                    string itemID = empDetail.itemID;
                    int qty = Convert.ToInt32(empDetail.reqQty);
                    addRecordForNewDeptReqOutOfPeriod(deparmentID, itemID, qty, empReq);
                }
            }
        }
        public static void updateQtyForExistingDeptReqWithinPeriod(string deptReqID, int qty)
        {
            DepartmentRequisition deptR = logic.DepartmentRequisitions.First(dpt => dpt.DeptRequisitionID == deptReqID);
            deptR.RequestedQuantity = deptR.RequestedQuantity + qty;
            logic.SaveChanges();
        }
        public static void addRecordForNewDeptReqWithinPeriod(string deparmentID, string itemID, int qty, EmployeeRequisition empReq)
        {
            DepartmentRequisition deptWithNewID1 = getNewDeptReqObject();

            DepartmentRequisition newdeptR = new DepartmentRequisition();
            newdeptR.DeptRequisitionID = deptWithNewID1.DeptRequisitionID;
            newdeptR.ApproveDate = p.StartDate;
            newdeptR.DepartmentID = deparmentID;
            newdeptR.ItemID = itemID;
            newdeptR.RequestedQuantity = qty;
            newdeptR.AfterDeliveredQuantity = 0;
            newdeptR.AllocatedQuantity = 0;
            newdeptR.OutstandingDeliveredQty = 0;
            newdeptR.ExactApproveDate = DateTime.Now;
            logic.AddToDepartmentRequisitions(newdeptR);
            logic.SaveChanges();

            DepartmentRequisitionDetail newdeptReDetail = new DepartmentRequisitionDetail();
            DepartmentRequisitionDetail deptDetailWithNewID1 = getNewDeptReqDetailObject();

            newdeptReDetail.DepartmentRequisitionDetailID = deptDetailWithNewID1.DeptRequisitionID;
            newdeptReDetail.DeptRequisitionID = newdeptR.DeptRequisitionID;
            newdeptReDetail.EmployeeRequisitionID = empReq.EmployeeRequisitionID;
            logic.AddToDepartmentRequisitionDetails(newdeptReDetail);
            logic.SaveChanges();
        }
        public static void addRecordForNewDeptReqOutOfPeriod(string deparmentID, string itemID, int qty, EmployeeRequisition empReq)
        {

            DepartmentRequisition newdeptR = new DepartmentRequisition();
            DepartmentRequisition deptWithNewID2 = getNewDeptReqObject();
            newdeptR.DeptRequisitionID = deptWithNewID2.DeptRequisitionID;
            p.StartDate = p.EndDate.AddDays(1);
            newdeptR.ApproveDate = p.StartDate;
            newdeptR.DepartmentID = deparmentID;
            newdeptR.ItemID = itemID;
            newdeptR.RequestedQuantity = qty;
            newdeptR.AfterDeliveredQuantity = 0;
            newdeptR.AllocatedQuantity = 0;
            newdeptR.OutstandingDeliveredQty = 0;
            newdeptR.ExactApproveDate = DateTime.Now;
            logic.AddToDepartmentRequisitions(newdeptR);
            logic.SaveChanges();

            DepartmentRequisitionDetail newdeptReDetail = new DepartmentRequisitionDetail();
            DepartmentRequisitionDetail deptDetailWithNewID1 = getNewDeptReqDetailObject();

            newdeptReDetail.DepartmentRequisitionDetailID = deptDetailWithNewID1.DeptRequisitionID;
            newdeptReDetail.DeptRequisitionID = newdeptR.DeptRequisitionID;
            newdeptReDetail.EmployeeRequisitionID = empReq.EmployeeRequisitionID;
            logic.AddToDepartmentRequisitionDetails(newdeptReDetail);
            logic.SaveChanges();
        }

        public static DepartmentRequisition getNewDeptReqObject()
        {
            var deptID = (from ad in logic.DepartmentRequisitions select ad.DeptRequisitionID).Max();

            string newObj = deptID;

            DepartmentRequisition deptReqWithNewID = new DepartmentRequisition();

            if (newObj == "")
            {
                deptReqWithNewID = ManageRequestManager.generateDepFirstID();
            }
            else
            {
                deptReqWithNewID = ManageRequestManager.generateDepID(newObj);
            }
            return deptReqWithNewID;
        }

        public static DepartmentRequisitionDetail getNewDeptReqDetailObject()
        {
            var deptID = (from ad in logic.DepartmentRequisitionDetails select ad.DepartmentRequisitionDetailID).Max();

            string newObj = deptID;

            DepartmentRequisitionDetail deptReqDetailWithNewID = new DepartmentRequisitionDetail();

            if (newObj == "")
            {
                deptReqDetailWithNewID = ManageRequestManager.generateDepDetailFirstID();
            }
            else
            {
                deptReqDetailWithNewID = ManageRequestManager.generateDepDetailID(newObj);
            }
            return deptReqDetailWithNewID;
        }

    }
}